package qs.openxt.libs.record;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Message;
import android.util.Log;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016/3/23.
 */
public class QTAudioRecorder {
    private AudioRecord mRecorder = null;
    public static final int SAMPLE_RATE = 16000;
    private short[] mBuffer;

    private boolean mIsRecording = false;
    final String path;

    private double volume = 0;

    public QTAudioRecorder(String path) {
        this.path = path;

        int bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
        mBuffer = new short[bufferSize];
        mRecorder = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
    }

    public void start() throws Exception {
        try {
            if (mIsRecording) {
                return;
            }

            if (mRecorder == null) {
                return;
            }
            mIsRecording = true;
            mRecorder.startRecording();
            startBufferedWrite(new File(path));
        }catch (Exception e){

        }
    }

    private void startBufferedWrite(final File file) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                DataOutputStream output = null;
                try {
                    output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
                    while (mIsRecording) {
                        int readSize = mRecorder.read(mBuffer, 0, mBuffer.length);

                        long v = 0;
                        // 将 buffer 内容取出，进行平方和运算
                        for (int i = 0; i < mBuffer.length; i++) {
                            v += mBuffer[i] * mBuffer[i];
                        }
                        // 平方和除以数据总长度，得到音量大小。
                        double mean = v / (double) readSize;
                        volume = Math.log10(mean);

                        for (int i = 0; i < readSize; i++) {
                            output.writeShort(mBuffer[i]);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (output != null) {
                        try {
                            output.flush();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                output.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }).start();
    }

    public void stop() throws Exception {
        try {
            if (mRecorder == null) {
                return;
            }
            if (!mIsRecording) {
                return;
            }
            mRecorder.stop();
            mRecorder.release();
            mIsRecording = false;
        }catch (Exception e){
            mRecorder = null;
        }
    }

    public double getAmplitude() {
        return volume;
    }
}
